package com.pengcheng.hnweb.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class RabbitUtil {
    @Autowired
    public RabbitTemplate rabbitTemplate;
    @Autowired
    public Common common;
    @Autowired
    public JdbcTemplate jdbcTemplate;
    @Autowired
    private FileUtil fileUtil;
    public void restart(String channel_id)
    {
        respon(channel_id,"0506200001554221");
    }
    public void clear_cache(String channel_id)
    {
        respon(channel_id,"0506200002AA0291");
    }
    public void open_iccd(String channel_id)
    {
        respon(channel_id,"05100714000204000100021051");
    }
    public void respon(String channel_id,String msg)
    {
        try {
            Thread.sleep(1000);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        Map<String,Object> res=new HashMap<>();
        res.put("channel_id",channel_id);
        res.put("msg",msg);
        rabbitTemplate.convertAndSend("hnww.respon",res);
//        String sql="insert into hnww_msg (channel,type,channel_id,msg,fdate) values (?,?,?,?,?)";
//        Object[] ps1={"hnww.respon",1,channel_id,msg,common.format_date(new Date())};
//        jdbcTemplate.update(sql,ps1);
        add_msg(channel_id,1+"",msg,new Date());
    }
    private void add_msg(String channel_id,String type,String msg,Date date)
    {
        fileUtil.add_msg(channel_id,common.format_date(date));
        fileUtil.add_msg(channel_id,type+":"+msg);
    }
}
